package util.content;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class GoogleStanselateUtil {
	private static HttpPost httppost;
	private static HttpResponse response;
	private static DefaultHttpClient httpclient = new DefaultHttpClient();
	HttpParams params = httpclient.getParams();

	/*
	 * @param text 翻译原文
	 * 
	 * @param lanFrom 源语言
	 * 
	 * @param lanTo 目标语言
	 * 
	 * @return 翻译后的字符串
	 */
	public String translate(int i) {
		String[] LanguageList = { "en", "zh-TW", "th", "tr", "ko", "nl", "vi", "pt", "it", "pl", "uk", "fa", "fr", "de", "ru", "ja", "es" };
		String lanFrom = null;
		String lanTo = LanguageList[i];
		if (i == 0 || i == 1) {
			lanFrom = "zh-CN";
		} else {
			lanFrom = "en";
		}
		String content = "";
		while (true) {
			List<NameValuePair> qparams = new ArrayList<NameValuePair>();
			qparams.add(new BasicNameValuePair("hl", "en"));
			qparams.add(new BasicNameValuePair("ie", "UTF-8"));
			qparams.add(new BasicNameValuePair("sl", lanFrom));
			qparams.add(new BasicNameValuePair("text", text));
			qparams.add(new BasicNameValuePair("tl", lanTo));

			// HttpClient提交post请求
			httppost = new HttpPost("http://translate.google.com/translate_t#");
			String responseBody = "";
			try {
				// 将参数封装到post数据包中，
				httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8));
				HttpConnectionParams.setConnectionTimeout(params, 700000);
				HttpConnectionParams.setSoTimeout(params, 1000000);
				response = httpclient.execute(httppost);
				responseBody = EntityUtils.toString(response.getEntity());
				// 过滤出所需翻译后的内容
				int tmp1 = responseBody.indexOf("result_box");
				int tmp2 = responseBody.indexOf(">", tmp1);
				int tmp3 = responseBody.indexOf("</div>", tmp2);
				content = responseBody.substring(tmp2 + 1, tmp3).replaceAll("<br>", "\n").replaceAll("<[^>]*>", "");// 替换换行符和其他网页标签
				content = content.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll("&apos;", "'").replaceAll("&quot;", "\"").replaceAll("&#160;", " ").replaceAll("&#39;", "'").replaceAll("&#12316;", "〜").replaceAll("</\\s*", "</");
				content = content.replaceAll("\\!\\s*", "!").replaceAll("/\\s*", "/").replaceAll("\\.\\s*", ".").replaceAll(",\\s*", ",").replaceAll("\\?\\s*", "?").replaceAll("\\(\\s*", "(").replaceAll("\\)\\s*", ")").replaceAll("\\{\\s*", "{").replaceAll("\\}\\s*", "}").replaceAll("\\[\\s*", "[").replaceAll("\\] ", "]").replaceAll("-\\s*", "-").replaceAll("\\|\\s*", "|").replaceAll("@\\s*", "@").replaceAll("#\\s*", "#").replaceAll("\\$\\s*", "\\$").replaceAll("\\%\\s*", "\\%").replaceAll("\\^\\s*", "\\^").replaceAll("\\&\\s*", "\\&").replaceAll("\\*\\s*", "\\*").replaceAll("&amp;\\s*", "&");
				break;
			} catch (Exception e) {
				System.out.println("google 连接出现错误");
				continue;
			} finally {
				httppost.abort();
			}
			
		}
		return content;
	}

	String text = null;

	public void setText(String text) {
		// System.out.println(text);
		this.text = text;
	}
}